<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /路线列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部路线</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">目的地</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li :class="{active:Number(oneIndex)==-1 && Number(twoIndex)==-1}">
                  <a title="全部" href="#" @click="initRoute()">全部</a>
                </li>
                <li v-for="(item,index) in destinationList" :key="index" :class="{active:oneIndex==index}">
                  <a :title="item.title" href="#" @click="searchOne(item.id,index)">{{item.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li v-for="(item,index) in nationList" :key="index" :class="{active:twoIndex==index}">
                  <a :title="item.title" href="#" @click="searchTwo(item.id, index)">{{item.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
              <li :class="{'current bg-orange' : Number(sort)==3}">
                <a title="关注度" href="#" @click="orderByBuyCount()">关注度</a>
              </li>
              <li :class="{'current bg-orange' : Number(sort)==2}">
                <a title="最新" href="#" @click="orderByGmtCreate()">最新</a>
              </li>
              <li :class="{'current bg-orange' : Number(sort)==1}">
                <a title="价格" href="#" @click="orderByPrice()">价格&nbsp;
                  <span>↓</span>
                </a>
              </li>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="data.total==0">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam">没有相关数据，小编正在努力整理中...</span>
          </section>
          <!-- /无数据提示 结束-->
          <article class="comm-course-list">
            <ul class="of" id="bna">
              <li v-for="(route,index) in data.list" :key="index">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img :src="route.cover" class="img-responsive" :alt="route.title">
                    <div class="cc-mask">
                      <a :href="'/route/' + route.id" :title="route.title" class="comm-btn c-btn-1">立即购买</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a :href="'/route/' + route.id" :title="route.title" class="course-title fsize18 c-333">{{route.title}}</a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <!-- <span class="fr jgTag bg-green" v-if="Number(route.price===0)">
                      <i class="c-fff fsize12 f-fA">免费</i>
                    </span> -->
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{route.day}}天</i>
                      |
                      <i class="c-999 f-fA">￥{{route.price}}起</i>
                      |
                      <i class="c-999 f-fA">已售:{{route.buyCount}}</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"></div>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <div>
          <div class="paging">
            <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
            <a
              :class="{undisable: !data.hasPrevious}"
              href="#"
              title="首页"
              @click.prevent="gotoPage(1)">首</a>
            <a
              :class="{undisable: !data.hasPrevious}"
              href="#"
              title="前一页"
              @click.prevent="gotoPage(data.current-1)">&lt;</a>
            <a
              v-for="page in data.pages"
              :key="page"
              :class="{current: data.current == page, undisable: data.current == page}"
              :title="'第'+page+'页'"
              href="#"
              @click.prevent="gotoPage(page)">{{ page }}</a>
            <a
              :class="{undisable: !data.hasNext}"
              href="#"
              title="后一页"
              @click.prevent="gotoPage(data.current+1)">&gt;</a>
            <a
              :class="{undisable: !data.hasNext}"
              href="#"
              title="末页"
              @click.prevent="gotoPage(data.pages)">末</a>
            <div class="clear"/>
          </div>
        </div>
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /路线列表 结束 -->
  </div>


</template>
<script>
import routeApi from "@/api/route"
export default {

  data(){
    return{
      page:1, //当前页
      data:{},  //目的地列表
      destinationList: [], // 一级分类列表
      nationList: [], // 二级分类列表

      searchObj: {
        
      }, // 查询表单对象

      oneIndex:-1,
      twoIndex:-1,
      sort: 1
    }
  },
  created(){
    this.initRoute()
    this.initDestination()
    this.orderByPrice()
  },
  watch: {
    
  },
  methods: {
    orderByBuyCount(){
      this.sort = 3
      this.searchObj.buyCount = "buyCount"
      this.gotoPage(1)
    },
    orderByGmtCreate(){
      this.sort = 2
      this.searchObj.gmtCreate = "gmtCreate"//只要不为空就行
      this.searchObj.buyCount=""
      this.searchObj.price = ""
      this.gotoPage(1)
    },
    orderByPrice(){
      this.sort = 1
      this.searchObj.price = "price"//不为空就行
      this.searchObj.buyCount=""
      this.searchObj.gmtCreate = ""
      this.gotoPage(1)
    },
    //1 初始化页面，包括一级分类、路线列表第一页
    initRoute(){
      this.searchObj.destParentId = ""
      this.searchObj.destId = ""
      this.searchObj
      routeApi.getRouteList(1,8,this.searchObj)
        .then(response => {
          //consol.log("查询到的路线列表",response)
          this.data = response.data.data
          this.oneIndex = -1
          this.twoIndex = -1
          this.nationList = []
        })
    },
    //2 初始化目的地分类
    initDestination(){
      routeApi.getDestinationList()
        .then(response => {
          console.log("获取到目的地分类",response)
          this.destinationList = response.data.data.list
        })
    },
    //3 分页切换的方法
    gotoPage(page) {
      routeApi.getRouteList(page,8,this.searchObj).then(response => {
        this.data = response.data.data
      })
    },
    //4 点击一级分类，查询二级分类，跟之前的一样，将传过来的id跟所有的一级分类id做比较
    searchOne(id, index){
      //将传过来的index赋值给oneIndex，是为了让样式生效
      this.oneIndex = index
      //其他值还是原始值
      this.twoIndex = -1
      this.searchObj.destId = ""
      this.nationList = []

      //把一级分类点击id值，赋值给searchObj
      this.searchObj.destParentId = id
      //点击某个一级分类进行条件查询
      //this.gotoPage(1)

      for(var i = 0; i < this.destinationList.length; i++){
        //如果相等则查询对应的二级分类
        var region = this.destinationList[i]
        if(id == region.id){
          this.nationList = region.children
        }
      }
      this.gotoPage(1)
    },
    //5 点击二级分类
    searchTwo(destId, index){
      //赋值index，为了样式生效
      this.twoIndex = index
      //把二级分类的id传给searchObj
      this.searchObj.destId = destId
      this.gotoPage(1)
    }
  }

};
</script>
<style scoped>
  .active {
    background: #bdbdbd;
  }
  .hide {
    display: none;
  }
  .show {
    display: block;
  }
</style>